package com.homegoods.orderinfodetail.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.homegoods.orderinfodetail.R;
import com.homegoods.orderinfodetail.item.SpeclistItem;
import com.homegoods.orderinfodetail.model.GoodsDetailTypeBean;
import com.homegoods.orderinfodetail.model.SpeclistBean;
import com.homegoods.resource.SpacesItemDecoration;
import com.kedll.library.adapter.MultiSelectAdapter;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.adapter.MyArrayAdapter;
import com.kedll.library.util.MyUtils;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author QiQi
 * @ClassName: GoodsParamAdapter
 * @date 2018/9/22
 * @description
 */
public class GoodsParamAdapter extends MyArrayAdapter<GoodsDetailTypeBean> {
    private OnItemClickListener onItemClickListener;

    public GoodsParamAdapter(Context context) {
        super(context);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.info_item_goods_type, parent, false);
            holder = new ViewHolder(convertView);
            holder.mList.setNestedScrollingEnabled(false);
            holder.mList.setLayoutManager(MyUtils.getFlexBoxLayoutManager(getContext()));
            holder.mList.setHasFixedSize(false);
            holder.mList.addItemDecoration(new SpacesItemDecoration(3, 4));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mParamName.setText(getItem(position).getName());

        MultiSelectAdapter<SpeclistBean> selectAdapter = new MultiSelectAdapter<>(false);
        holder.mList.setAdapter(selectAdapter);

        selectAdapter.setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<SpeclistBean>) (v, iItem) -> {
            if (onItemClickListener != null) {
//                onItemClickListener.onItemClick(iItem.getData(), getItem(position).getName());
                onItemClickListener.onItemClick(iItem.getData(), getItem(position).getName());
            }
        });

        Observable.fromIterable(getItem(position).getSpeclist())
                .map(speclistBean -> speclistBean.createItem(selectAdapter))
                .toList()
//                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(speclistItems -> selectAdapter.setItems(speclistItems));

        return convertView;
    }

    public interface OnItemClickListener {
        void onItemClick(SpeclistBean speclistBean, String titleName);

    }

    public static class ViewHolder {
        TextView mParamName;
        RecyclerView mList;

        public ViewHolder(View view) {
            mParamName = view.findViewById(R.id.tv_param_name);
            mList = view.findViewById(R.id.list);
        }
    }
}
